package com.tensquare.friend.controller;

import com.tensquare.friend.client.UserClient;
import com.tensquare.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import jdk.net.SocketFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2021/6/21.
 */
@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private FriendService friendService;

    @Autowired
    private UserClient userClient;



    /**
     *
     * @param friendid 好友或者非好友id
     * @param type  0:不喜欢 1:喜欢
     * @return
     */
    @RequestMapping(value="/like/{friendid}/{type}",method = RequestMethod.PUT)
    public Result addFriend(@PathVariable String friendid,@PathVariable String type){
        //判断是否已登录
       Claims claims = (Claims)request.getAttribute("user_claims");
       if (claims == null){
           return new Result(false, StatusCode.ACCESSS_ERROR,"无权限访问");
       }else{
           //从token中获取用户信息
           String userId = claims.getId();
            if (type.equals("1")){
                //如果喜欢  添加好友
                int flag = friendService.addFriend(userId,friendid);
                if (flag == 0){
                    return new Result(false ,StatusCode.ERROR,"不能重复添加好友");
                }
                if (flag == 1){
                    userClient.updateFanscountAndFollowcount(userId,friendid,1);
                    return new Result(true ,StatusCode.OK,"添加成功");
                }
            }else{
                //如果不喜欢 添加非好友
               int flag =  friendService.addNoFriend(userId,friendid);
                if (flag == 0){
                    return new Result(false ,StatusCode.ERROR,"不能重复添加非好友");
                }
                if (flag == 1){
                    return new Result(true ,StatusCode.OK,"添加非好友成功");
                }
            }
       }
        return new Result(true ,StatusCode.OK,"操作成功");
    }

    /**
     * 删除好友
     * @param friendid
     * @return
     */
    @RequestMapping(value="/delete/{friendid}",method = RequestMethod.DELETE)
    public Result deleteFriend(@PathVariable String friendid){
        //判断是否已登录
        Claims claims = (Claims)request.getAttribute("user_claims");
        if (claims == null){
            return new Result(false, StatusCode.ACCESSS_ERROR,"无权限访问");
        }
        //从token中获取用户信息
        String userId = claims.getId();
        friendService.deleteFriend(userId,friendid);
        //自己的关注数减1,别人的粉丝数减1
        userClient.updateFanscountAndFollowcount(userId,friendid,-1);
        return new Result(true, StatusCode.OK,"删除成功");
    }

}
